{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n",
      "------------------------------------------------------------\n",
      "0\n",
      "['1'] 1\n",
      "['1', '2'] 2\n",
      "------------------------------------------------------------\n",
      "i\n",
      "a\n",
      "------------------------------------------------------------\n",
      "Provide a word to search for vowels:212\n",
      "------------------------------------------------------------\n",
      "[1, 2, 3, 4]\n",
      "[1, 2, 4]\n",
      "[1, 2]\n",
      "[2]\n",
      "[2, 3, 4]\n",
      "[1, 2, 3, 4]\n",
      "------------------------------------------------------------\n",
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n",
      "------------------------------------------------------------\n",
      "[1, 4, 9, 16, 25, 36]\n",
      "[1, 4, 9, 16, 25, 36, 49] [1, 4, 9, 16, 25, 36, 49]\n",
      "[1, 4, 9, 16, 25, 36, 49]\n",
      "[1, 4, 9, 16, 25, 36, 49] [1, 4, 9, 16, 25, 36, 49, 64]\n",
      "------------------------------------------------------------\n",
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n",
      "------------------------------------------------------------\n",
      "\t M\n",
      "\t a\n",
      "\t r\n",
      "\t v\n",
      "\t i\n",
      "\t n\n",
      "------------------------------------------------------------\n",
      "\t M\n",
      "\t a\n",
      "\t r\n",
      "\t v\n",
      "\t i\n",
      "\t n\n",
      "\n",
      "\t\t A\n",
      "\t\t n\n",
      "\t\t d\n",
      "\t\t r\n",
      "\t\t o\n",
      "\t\t i\n",
      "\t\t d\n",
      "\n",
      "\t\t\t P\n",
      "\t\t\t a\n",
      "\t\t\t r\n",
      "\t\t\t a\n",
      "\t\t\t n\n",
      "\t\t\t o\n",
      "\t\t\t i\n",
      "\t\t\t d\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# vowels.py\n",
    "vowels = [\"a\", \"e\", \"i\", \"o\", \"u\"]\n",
    "word = \"Milliways\"\n",
    "# 判断一个对象是否在另一个对象当中，用 in 来检查\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        print(i)\n",
    "print(\"------------------------------------------------------------\")\n",
    "\n",
    "# 创建一个空列表\n",
    "found = []\n",
    "# len() 方法返回对象(字符，列表，元组等)的长度或项目个数\n",
    "print(len(found))  # 返回0\n",
    "\n",
    "# append() 方法在列表的末尾添加一个对象\n",
    "found.append(\"1\")\n",
    "print(found, len(found))\n",
    "\n",
    "# 判断一个对象是否包含在另一个集合里，还可以使用 not in 来检查\n",
    "if \"2\" not in found:\n",
    "    found.append(\"2\")\n",
    "print(found, len(found))\n",
    "print(\"------------------------------------------------------------\")\n",
    "\n",
    "\n",
    "# vowels2.py\n",
    "vowels = [\"a\", \"e\", \"i\", \"o\", \"u\"]\n",
    "word = \"Milliways\"\n",
    "found = []\n",
    "for i in word:  # 循环遍历 Milliways\n",
    "    if i in vowels:  # 判断 Milliways 是否在 vowels 列表当中\n",
    "        if i not in found:  # 判断 i i a 是否在 found 列表当中\n",
    "            found.append(i)  # 如果不在，将 i a 添加到 found 列表当中(重复出现的append不会执行)\n",
    "for k in found:  # 当执行完第一个循环之后，遍历 found 列表中的元素\n",
    "    print(k)\n",
    "print(\"------------------------------------------------------------\")\n",
    "\n",
    "# vowels3.py\n",
    "# 在 vowels2.py 的基础上添加用户输入功能\n",
    "vowels = [\"a\", \"e\", \"i\", \"o\", \"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = []\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        if i not in found:\n",
    "            found.append(i)\n",
    "for k in found:\n",
    "    print(k)\n",
    "print(\"------------------------------------------------------------\")\n",
    "\n",
    "# remove() 方法从列表中删除对象\n",
    "nums = [1, 2, 3, 4]\n",
    "print(nums)\n",
    "nums.remove(3)  # 3 不是索引，是要删除的值\n",
    "print(nums)\n",
    "\n",
    "# pop() 方法根据对象的索引值从现有列表删除和返回一个对象\n",
    "nums.pop()  # 默认删除最后一个,并且返回删除对象\n",
    "print(nums)\n",
    "nums.pop(0)  # 删除第一个对象,且返回删除对象\n",
    "print(nums)\n",
    "\n",
    "# extend() 方法接受第二个列表，将其中的值添加到现有的列表当中\n",
    "nums.extend([3, 4])  # 将[3,4] 添加到 nums 列表当中\n",
    "print(nums)\n",
    "\n",
    "# insert() 方法将对象插入到现有列表当中指定的索引值前面\n",
    "# insert 不能将对象插入到列表末尾，要使用append\n",
    "nums.insert(0, 1)  # (索引值, 对象)\n",
    "print(nums)\n",
    "print(\"------------------------------------------------------------\")\n",
    "\n",
    "\n",
    "# panic.py\n",
    "# Don't panic! --> on tap\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)  # 将字符串转换成列表\n",
    "print(phrase)\n",
    "print(plist)\n",
    "for i in range(4):  # 利用for循环，删除最后4个字符串 n i c !\n",
    "    plist.pop()\n",
    "plist.remove(\"D\")  # 移除 D\n",
    "plist.remove(\"'\")  # 移除 '\n",
    "plist.insert(2, plist.pop(3))  # 将空格移到 t 的前面\n",
    "plist.extend([plist.pop(), plist.pop()])  # 交换列表末尾的两个对象的位置\n",
    "new_plist = \"\".join(plist)  # 将列表转换成字符串\n",
    "print(plist)\n",
    "print(new_plist)\n",
    "print(\"------------------------------------------------------------\")\n",
    "\n",
    "\n",
    "# 列表的引用\n",
    "# 将一个列表复制到另一个列表 用 copy() 方法\n",
    "first = [1, 4, 9, 16, 25, 36]\n",
    "second = first\n",
    "print(second)\n",
    "second.append(49)\n",
    "print(first, second)  # first列表和second列表同时变化\n",
    "\n",
    "# 为了避免上述情况，用 copy() 方法\n",
    "third = second.copy()\n",
    "print(third)\n",
    "third.append(64)\n",
    "print(second, third)  # third列表比second列表多了64\n",
    "print(\"------------------------------------------------------------\")\n",
    "\n",
    "\n",
    "# panic2.py\n",
    "# Don't panic! --> on tap\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "new_phrase = (\"\".join(plist[1:3])) + \\\n",
    "    (\"\".join([plist[5], plist[4], plist[7], plist[6]]))\n",
    "print(new_phrase)\n",
    "\n",
    "# 对比panic.py 和 panic2.py\n",
    "# panic.py 使用列表的方法来处理 而 panic2.py 用的是切片的方法\n",
    "# 列表的方法会改变一个列表的原始状态，而切片通常不会改变列表的状态\n",
    "print(\"------------------------------------------------------------\")\n",
    "\n",
    "\n",
    "# for 循环了解列表\n",
    "# marvin.py\n",
    "paranoid_android = \"Marvin\"\n",
    "letters = list(paranoid_android)\n",
    "for i in letters:\n",
    "    print('\\t', i)  # \\t 一个tab键，四个空格\n",
    "print(\"------------------------------------------------------------\")\n",
    "\n",
    "\n",
    "# for 循环了解切片\n",
    "# mavin2.py\n",
    "paranoid_android = \"Marvin, the Paranoid Android\"\n",
    "letters = list(paranoid_android)\n",
    "for i in letters[:6]:\n",
    "    print('\\t', i)\n",
    "print()\n",
    "for j in letters[-7:]:\n",
    "    print('\\t'*2, j)\n",
    "print()\n",
    "for k in letters[12:20]:\n",
    "    print('\\t'*3, k)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
